//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer.ServiceModel
{
    using System;
    using System.Collections.ObjectModel;
    using Microsoft.Research.DataLayer;
    using System.Globalization;

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DetailedWorkflowModel : BaseModel
    {
        /// <summary>
        /// 
        /// </summary>
        private Collection<ParameterDescriptionModel> workflowRequiredParameters = new Collection<ParameterDescriptionModel>();

        /// <summary>
        /// 
        /// </summary>
        private Collection<NodeModel> nodes = new Collection<NodeModel>();

        /// <summary>
        /// Gets the workflow required parameters.
        /// </summary>
        /// <value>The workflow required parameters.</value>
        public Collection<ParameterDescriptionModel> WorkflowRequiredParameters
        {
            get
            {
                return this.workflowRequiredParameters;
            }
        }

        /// <summary>
        /// Gets the nodes.
        /// </summary>
        /// <value>The nodes.</value>
        public Collection<NodeModel> Nodes
        {
            get
            {
                return this.nodes;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DetailedWorkflowModel"/> class.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        public DetailedWorkflowModel(Activity workflow)
        {
            this.Id = (workflow as IObject).ID;
            this.Name = workflow.Name;
            this.Label = string.IsNullOrEmpty(workflow.VersionLabel) ?
              this.Name : string.Format(CultureInfo.CurrentUICulture, "{0} ({1})", this.Name, workflow.VersionLabel);
            this.GetWorkflowRequiredParams(workflow);
        }

        /// <summary>
        /// Gets the workflow required params.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        private void GetWorkflowRequiredParams(Activity workflow)
        {
            if (null == this.workflowRequiredParameters)
            {
                this.workflowRequiredParameters = new Collection<ParameterDescriptionModel>();
            }

            this.workflowRequiredParameters.Clear();
            foreach (ActivitySequence activity in workflow.Children[0].ActivitySequences)
            {
                this.GetRequiredParamsFromSequence(activity);
            }
        }

        /// <summary>
        /// Gets the required params from sequence.
        /// </summary>
        /// <param name="sequence">The sequence.</param>
        private void GetRequiredParamsFromSequence(ActivitySequence sequence)
        {
            // Retrieve parameter assignments.
            foreach (ParameterAssignment parameterAssignment in sequence.ParameterAssignments)
            {
                if (parameterAssignment.RequiredForWorkflow && parameterAssignment.ActivityParameter.Direction == DirectionType.Input)
                {
                    ParameterDescriptionModel paramModel = new ParameterDescriptionModel(parameterAssignment);
                    paramModel.ActivityUniqueId = sequence.Name;
                    this.workflowRequiredParameters.Add(paramModel);
                }
            }

            // Recurse through its children.
            foreach (ActivitySequence childSequence in sequence.ActivitySequences)
            {
                this.GetRequiredParamsFromSequence(childSequence);
            }
        }
    }
}
